Function parameters of C#

In the previous section, we had taken a look at the tasks we briefly discuss the parameters, but in a short time the parameters are very simple and straightforward to use that can make them very powerful

The first thing we will take is outside RIF modifier. C # and other languages ​​are also different between two parameters: "value" and "by reference" is the default in "core value" in C #, which basically means that when you pass a variable in a function call So, in the context of actually sending a copy of that object, it also means that you can pass through the function without affecting the original object. You can change the master.

With referees and keywords, we can change this behavior, so we give it according to our price reference.

The ref modifier

Consider the following example:


static void Main(string[] args)
{
    int number = 20;
    AddFive(number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(int number)
{
    number = number + 5;
}

We make an integer, give the number 20 for it, and then we use the AddFive () method, which should add 5 into the number. But what does this do? No. Inside the function we assign numbers, never outside the function, because we have passed a copy of the number value instead of its reference. It works in C #, and in many cases, this is a favorite result, however, in this case, we really want to modify the number inside our function. Enter ref keyword:


static void Main(string[] args)
{
    int number = 20;
    AddFive(ref number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(ref int number)
{
    number = number + 5;
}

As you can see, whatever we have done, it is adding the function to the function and adding it to the function call. If you run the program now, you will see that the value of the number has changed now, when we come back from the function call.

The out modifier

Out Modifier works a lot like the referee modifier, both of them ensure that the parameters have been passed from the context rather than the value, but they come with two important differences: the method was given to a reserve moderator before calling Price should be started - this is outside the moderator is not right, where you can use un-initial values, on the other hand, specify the value Without A can not leave the call function with out parameters. Since you can pass non-initial values ​​as an out parameter, you are not actually able to use a parameter inside a function - you can only specify a new value for it.

Whether the use or reef really depends on the situation, as you will understand that once you start using them. Generally, both are used only to work on the issue of being able to return values ​​from the function, with C #

Out use modifier is to use the correct reef modifier, as shown above. Just change the keyword keyword for the keyword

The params modifier

So far, all our actions have accepted certain standards. However, in some cases, you may need a function, in which there is an arbitrary number of parameters, of course, by applying an array or a list as a parameter, such as:


static void GreetPersons(string[] names) { }

However, calling it would be a bit clumsy. In the shortest form, it would look like this:


GreetPersons(new string[] { "John", "Jane", "Tarzan" });

It is acceptable, but it can be done even smarter, with the params keyword:


static void GreetPersons(params string[] names) { }

Calling it would then look like this:


GreetPersons("John", "Jane", "Tarzan");

Another advantage of using the Paramix approach is that you have permission to pass it to zero parameters.

Factors with parameters can also take other parameters, as long as the parameter parameter is final, only one parameter using the parameter keyword can be used for each function. Here's a final and more complete example:


static void Main(string[] args)
{
    GreetPersons(0);
    GreetPersons(25, "John", "Jane", "Tarzan");
    Console.ReadKey();
}

static void GreetPersons(int someUnusedParameter, params string[] names)
{
    foreach(string name in names)
        Console.WriteLine("Hello, " + name);
}